package sn_gm_report

import grails.converters.JSON

/**
 * GM��equipment status Monitor controller
 */
class TopoController {

    def dynamicQueryService

    def topo() {

    }

    /**
     * 获取eqp
     * @return
     */
    def getEqpStatus() {


        params.SYS_QUERY_NAME = 'getEqpStatus'
        def getEqpStatus = dynamicQueryService.queryForList(params)
        print(getEqpStatus)
        render getEqpStatus as JSON
    }

    /**
     * 拖拽
     * @return
     */
    def changeLocation() {

        def id = params.id ?: 0
//        print(getClass(id))
        def p = DWR_eqpStatus.get(id)

        p.x = new BigDecimal(params.x)
        p.y = new BigDecimal(params.y)
        print("id:" + p.id + "x:" + p.x + "y:" + p.y)

        try {
            p.save(flush: true)
            if (p.hasErrors()) {
                println p.errors
                println "----------------"
            }
        }
        catch (Exception e) {
            // deal with excepti
            print(e.getMessage())
        }
//        if(!p.save()){
//            p.errors.allErrors.each{
//                println(it)
//            }
//        }
        println("Eqp_coordinate:" + params)
        render ""
    }
    /***
     * 放大缩小
     * @return
     */
    def deformation() {
        def id = params.id ?: 0
        def p = DWR_eqpStatus.get(id)

        if (params.act == '0') {
            p.width += 10
            p.height += 5

        } else if (params.act == '1') {
            if (p.width > 0 && p.height > 0) {
                p.width -= 10
                p.height -= 5
            }
        }

        print("eqpID:" + p.eqpID + " act" + params.act)

        try {
            p.save(flush: true)
            if (p.hasErrors()) {
                println p.errors
                println "----------------"
            }
        }
        catch (Exception e) {
            // deal with excepti
            print(e.getMessage())
        }
//        if(!p.save()){
//            p.errors.allErrors.each{
//                println(it)
//            }
//        }
    }

//    def addNode() {
//
//        def id = params.id?:0
//        def p = new DWR_Equipment(id)
//        data.state = params.state
//        data.update = params.update
//        data.x = new BigDecimal(params.x)
//        data.y = new BigDecimal(params.y)
//        data.type = params.type
//
//        dataList.put(params.ids, data)
//        println dataList
//        render dataList
//
//    }

    /**
     * �Ƴ��ڵ�
     * @return
     */
    def removeNode() {
        def id = params.id ?: 0
        def p = DWR_eqpStatus.get(id)
        p.delete()
    }

    def curEqpId() {
        params.SYS_QUERY_NAME = 'moduleList'
        def moduleList = dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME = 'eqpType'
        def eqptypeList = dynamicQueryService.queryForList(params)

        [moduleList: moduleList, eqptypeList: eqptypeList, mod: params.module ?: '', eqpType: params.eqpType]
    }

}